package com.ktg.mes.md.service.impl;


import java.util.List;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ktg.mes.md.mapper.MdStatisticsTypeBomMapper;
import com.ktg.mes.md.domain.MdStatisticsTypeBom;
import com.ktg.mes.md.service.IMdStatisticsTypeBomService;



/**
 * 统计分类的统计分类BOMService业务层处理
 *
 * @author byteco
 * @date 2023-02-22
 */
@Service
public class MdStatisticsTypeBomServiceImpl extends ServiceImpl<MdStatisticsTypeBomMapper, MdStatisticsTypeBom> implements IMdStatisticsTypeBomService
{




    /**
     * 查询统计分类的统计分类BOM
     *
     * @param entity 统计分类的统计分类BOM
     * @param page 分页对象
     * @return 统计分类的统计分类BOM
     */
    @Override
    public IPage<MdStatisticsTypeBom> queryPageList(MdStatisticsTypeBom entity, Page<MdStatisticsTypeBom> page) {
        QueryWrapper<MdStatisticsTypeBom> lqw = new QueryWrapper<MdStatisticsTypeBom>(entity);
        lqw.orderByDesc("create_time");
        IPage<MdStatisticsTypeBom> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询统计分类的统计分类BOM
     *
     * @param entity 统计分类的统计分类BOM
     * @return 统计分类的统计分类BOM
     */
    @Override
    public List<MdStatisticsTypeBom> queryList(MdStatisticsTypeBom entity) {
        QueryWrapper<MdStatisticsTypeBom> lqw = new QueryWrapper<MdStatisticsTypeBom>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public MdStatisticsTypeBom getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增统计分类的统计分类BOM
     *
     * @param entity 统计分类的统计分类BOM
     * @return 结果
     */
    @Override
    public void insert(MdStatisticsTypeBom entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 修改统计分类的统计分类BOM
     *
     * @param entity 统计分类的统计分类BOM
     * @return 结果
     */
    @Override
    public boolean update(MdStatisticsTypeBom entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除统计分类的统计分类BOM
     *
     * @param bomIds 需要删除的统计分类的统计分类BOM主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] bomIds)
    {
        if (bomIds.length == 1){
            return delete(bomIds[0]);
        }
        for (int i = 0; i < bomIds.length; i++) {
            delete(bomIds[i]);
        }
        return true;
    }

    /**
     * 删除统计分类的统计分类BOM信息
     *
     * @param bomId 统计分类的统计分类BOM主键
     * @return 结果
     */
    @Override
    public boolean delete(Long bomId)
    {
        MdStatisticsTypeBom bom = getById(bomId);

        boolean istr = this.removeById(bomId);
        if (istr){
            getBaseMapper().updateStatisticsTypeBomString(bom.getParentId());
        }
        return istr;
    }


    /**
     * 更新 统计分类BOM字符
     * @param parentId
     */
    public void updateStatisticsTypeBomString(Long parentId){
        getBaseMapper().updateStatisticsTypeBomString(parentId);
    }
    private LambdaQueryWrapper<MdStatisticsTypeBom> buildQueryWrapper(MdStatisticsTypeBom entity) {
        LambdaQueryWrapper<MdStatisticsTypeBom> lqw = Wrappers.lambdaQuery();
                    lqw.like(StringUtils.isNotBlank(entity.getStatisticsName()), MdStatisticsTypeBom::getStatisticsName, entity.getStatisticsName());
                    lqw.eq(entity.getQuantity() != null, MdStatisticsTypeBom::getQuantity, entity.getQuantity());
        return lqw;
    }
}
